package com.wavehi.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;

/**
 * @Author WaveHi
 * @Date 28/9/2021 上午 11:15
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@WebListener("visitorStatisticListener")
public class VisitorStatisticListener implements HttpSessionListener {
    //Accept the number of user online
    private static Long numberOnline = 0L;
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        //Add
        numberOnline++;
        //Bind number to ServletContext
        se.getSession().getServletContext().setAttribute("numberOnline", numberOnline);
        System.out.println(numberOnline);

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        //Subtract
        numberOnline--;
        //Bind number to ServletContext
        se.getSession().getServletContext().setAttribute("numberOnline", numberOnline);
        System.out.println(numberOnline);
    }
}
